package com.ruoyi.web.controller.biz;

import com.ruoyi.biz.domain.BizCourse;
import com.ruoyi.biz.domain.BizCourseDetail;
import com.ruoyi.biz.service.IBizCourseDetailService;
import com.ruoyi.biz.service.IBizCourseService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 课程列Controller
 * 
 * @author ruoyi
 * @date 2021-08-10
 */
@Controller
@RequestMapping("/biz/coursedetail")
public class BizCourseDetailController extends BaseController
{
    private String prefix = "biz/coursedetail";

    @Autowired
    private IBizCourseDetailService bizCourseDetailService;

    @Autowired
    IBizCourseService courseService;

    @RequiresPermissions("biz:coursedetail:view")
    @GetMapping()
    public String coursedetail()
    {
        return prefix + "/coursedetail";
    }

    /**
     * 查询课程列列表
     */
    @RequiresPermissions("biz:coursedetail:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BizCourseDetail bizCourseDetail)
    {
        startPage();
        List<BizCourseDetail> list = bizCourseDetailService.selectBizCourseDetailList(bizCourseDetail);
        return getDataTable(list);
    }

    @RequiresPermissions("biz:coursedetail:list")
    @GetMapping("/detail/{courseId}")
    public String detail(@PathVariable("courseId") Long courseId, ModelMap mmap)
    {
        BizCourseDetail courseDetail = new BizCourseDetail();
        courseDetail.setCourseId(courseId);
        List<BizCourseDetail> courseDetailList = bizCourseDetailService.selectBizCourseDetailList(courseDetail);
        BizCourse             bizCourse        = courseService.selectBizCourseById(courseId);
        mmap.put("course", bizCourse);
        mmap.put("courseDetailList", courseDetailList);
        return prefix + "/coursedetail";
    }

    /**
     * 导出课程列列表
     */
    @RequiresPermissions("biz:coursedetail:export")
    @Log(title = "课程列", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BizCourseDetail bizCourseDetail)
    {
        List<BizCourseDetail> list = bizCourseDetailService.selectBizCourseDetailList(bizCourseDetail);
        ExcelUtil<BizCourseDetail> util = new ExcelUtil<BizCourseDetail>(BizCourseDetail.class);
        return util.exportExcel(list, "coursedetail");
    }

    /**
     * 新增课程列
     */
    @GetMapping({"/add/{id}","/add"})
    public String add(@PathVariable(value = "id", required = false) Long id,ModelMap mmap)
    {

        if (StringUtils.isNotNull(id))
        {
            mmap.put("course", courseService.selectBizCourseById(id));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存课程列
     */
    @RequiresPermissions("biz:coursedetail:add")
    @Log(title = "课程列", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BizCourseDetail bizCourseDetail)
    {
        return toAjax(bizCourseDetailService.insertBizCourseDetail(bizCourseDetail));
    }

    /**
     * 修改课程列
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        BizCourseDetail bizCourseDetail = bizCourseDetailService.selectBizCourseDetailById(id);
        mmap.put("bizCourseDetail", bizCourseDetail);
        return prefix + "/edit";
    }

    /**
     * 修改保存课程列
     */
    @RequiresPermissions("biz:coursedetail:edit")
    @Log(title = "课程列", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BizCourseDetail bizCourseDetail)
    {
        return toAjax(bizCourseDetailService.updateBizCourseDetail(bizCourseDetail));
    }

    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = bizCourseDetailService.selectCourseTree();
        return ztrees;
    }

    @GetMapping({"/video/{id}"})
    public String video(@PathVariable(value = "id", required = false) Long id,ModelMap mmap)
    {

        if (StringUtils.isNotNull(id))
        {
            mmap.put("courseDetail", bizCourseDetailService.selectBizCourseDetailById(id));
        }
        return prefix + "/video";
    }

    @GetMapping({"/addExam/{id}","/addExam"})
    public String addExam(@PathVariable(value = "id", required = false) Long id,ModelMap mmap)
    {

        if (StringUtils.isNotNull(id))
        {
            mmap.put("courseDetail", bizCourseDetailService.selectBizCourseDetailById(id));
        }
        return "biz/exam/exam";
    }

    @GetMapping({"/ppt/{id}"})
    public String ppt(@PathVariable(value = "id", required = false) Long id,ModelMap mmap)
    {

        if (StringUtils.isNotNull(id))
        {
            BizCourseDetail courseDetail = bizCourseDetailService.selectBizCourseDetailById(id);
            String          pptUrl       = courseDetail.getPptUrl();
            String          maxStr      = pptUrl.substring(pptUrl.lastIndexOf("/") + 1).replace(".png", "");
            List<String> imgList = new ArrayList<>();
            for (int i = 1; i <= Integer.valueOf(maxStr); i++) {
                imgList.add(courseDetail.getHost()+ pptUrl.replace(maxStr + ".png", i + ".png"));
            }
            mmap.put("courseDetail", courseDetail);
            mmap.put("imgList", imgList);
            mmap.put("total", maxStr);
        }
        return prefix + "/ppt";
    }
    /**
     * 删除课程列
     */
    @RequiresPermissions("biz:coursedetail:remove")
    @Log(title = "课程列", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(bizCourseDetailService.deleteBizCourseDetailByIds(ids));
    }

    @GetMapping(value = { "/selectManageTree/{id}", "/selectManageTree/" })
    public String selectManageTree(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("course", courseService.selectBizCourseById(id));
        }
        return prefix + "/tree";
    }
}
